<template>
  <ele-modal
    :width="750"
    :visible="visible"
    :confirm-loading="loading"
    :title="actionStatus == 'edit' ? '修改' : '新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }"
    >
      <a-row :gutter="24">
        <a-col :md="18" :sm="24" :xs="24">
          <a-form-item label="柜组名称" v-bind="validateInfos.name">
            <a-input
              allow-clear
              placeholder="请输入柜组名称"
              v-model:value="form.name"
              autocomplete="off"
            />
          </a-form-item>
          <!-- <a-form-item
            label="负责人"
            v-bind="validateInfos.charge_user_id"
          >
            <a-select
              :options="options"
              allow-clear
              placeholder="请选择负责人"
              v-model:value="form.charge_user_id"
              @change="changeUser"
              autocomplete="off"
            />
          </a-form-item> -->
			<a-form-item label="备注"
			  ><a-textarea
				allow-clear
				placeholder="请输入"
				v-model:value="form.note"
				autocomplete="off"
			/></a-form-item>
          <a-form-item label="排序">
            <a-input
              allow-clear
              placeholder="请输入排序"
              v-model:value="form.sort"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="状态">
            <a-switch v-model:checked="checked" @change="changeStatus" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { Form } from 'ant-design-vue';
  import { get_list } from '@/api/user/store_user';
  import { detail } from '@/api/material/store_group';
  import { get_store_user_select } from '@/api/material/select';

  // 表格列配置
  const useForm = Form.useForm;

  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean,
    // 编辑edit/新建add
    actionStatus: String,
    // 编辑数据id
    editDataId: [String, Number],
    storeId: [String, Number]
  });

  const emit = defineEmits(['done', 'update:visible']);

  // 提交状态
  const loading = ref(false);
  /* const storeData = ref([]); */
  const checked = ref(true);
	const options = ref([]);

  onMounted(() => {
    form.id = props.editDataId;
    form.store_code = props.storeId;
    get_store_user_select({page: 1,page_size: 99999,filters:{store_code:props.storeId}}).then((res) => {
		options.value = res;
    });
    if (props.editDataId) {
      detail({ id: props.editDataId }).then((res) => {
        form.store_code = res.store_code;
        form.name = res.name;
        /* form.charge_user_id = res.charge_user_id; */
        form.note = res.note;
        form.sort = res.sort;
        form.status = res.status;
        checked.value = res.status == 1 ? true : false;
      });
    }
  });

  const changeUser = (e) => {
    form.charge_user_id = e;
  };
  const changeStatus = (e) => {
    form.status = e ? '1' : '0';
  };

  // 表单数据
  const form = reactive({
    id: '',
    store_code: '',
    name: '',
    /* charge_user_id: null, */
	  note:'',
    sort: '',
    status: 1
  });
  // 表单验证规则
  const rules = reactive({
    name: [
      {
        required: true,
        message: '请输入班组名称',
        type: 'string',
        trigger: 'change'
      }
    ]
  });

  const change = (e) => {
    console.log('e', e);
    form.store_code = e;
  };

  const { validate, validateInfos } = useForm(form, rules);
  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        emit('done', form);
      })
      .catch(() => {});
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
</script>
<style scoped></style>
